//
//  BackgoundNotificaitonManager.m
//  OCTest
//
//  Created by lichenxi on 2022/1/20.
//


#import "BackgoundNotificaitonManager.h"

@implementation BackgoundNotificaitonManager

+ (instancetype)sharedInstance
{
    static BackgoundNotificaitonManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[BackgoundNotificaitonManager alloc] init];
    });
    return sharedInstance;
}

static BOOL _isAuthored = NO;

+(void) RequestAuthorization
{
    if (@available(iOS 10, *))
    {
        UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = [BackgoundNotificaitonManager sharedInstance];
        [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
            _isAuthored =granted;
            if (granted) {
                // 允许推送s
                NSLog(@"允许推送");
          
            }else{
                //不允许
                NSLog(@"不允许推送");
            }
            
        }];
    }
}


-(void) SendNotification:(char*)uniqueId:(char*)title:(char*)body :(int)channel {
    
    if (!@available(iOS 10, *)){
        return;
    }
    NSString* identifier = [NSString stringWithCString:uniqueId encoding:NSUTF8StringEncoding];
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
    content.badge = [NSNumber numberWithInt:-1];
    content.title = [NSString stringWithCString:title encoding:NSUTF8StringEncoding];
    content.body = [NSString stringWithCString:body encoding:NSUTF8StringEncoding];
    content.categoryIdentifier=@"GENERAL";
    
    if (channel>=2) {
        content.sound=[UNNotificationSound defaultSound];
    }else{
        content.sound=NULL;
    }
    
    
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.1
                                                                                                    repeats:NO];
    
    UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];

    [center addNotificationRequest:notificationRequest withCompletionHandler:^(NSError * _Nullable error)
    {
        if(error){
            NSLog(@"推送错误");
        }else{
            NSLog(@"推送成功 %@",identifier);
        }
    }];
}

-(void) CancelNotification:(char*)uniqueId{
    if (!@available(iOS 10, *)){
        return;
    }
    NSString* identifier = [NSString stringWithCString:uniqueId encoding:NSUTF8StringEncoding];
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    id removes = @[identifier];
    [center removePendingNotificationRequestsWithIdentifiers:removes];
    [center removeDeliveredNotificationsWithIdentifiers:removes];
//    [center removePendingNotificationRequestsWithIdentifiers: @[[NSString stringWithUTF8String: identifier]]];
}

#pragma mark - UNUserNotificationCenterDelegate
//将要推送

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    UNNotificationRequest *request = notification.request;
    UNNotificationContent *content = request.content;
    if(content.sound == NULL)
    {
        completionHandler(UNNotificationPresentationOptionNone);
    }else{
        completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
    }
}

//已经完成推送
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
    NSLog(@"============didReceiveNotificationResponse");
    NSString *categoryID = response.notification.request.content.categoryIdentifier;
    if ([categoryID isEqualToString:@"categoryIdentifier"]) {
        if ([response.actionIdentifier isEqualToString:@"enterApp"]) {
            if (@available(iOS 10.0, *)) {
                NSLog(@"enter app >= 10.0");
            } else {
                NSLog(@"enter app < 10.0");
                // Fallback on earlier versions
            }
        }else{
            NSLog(@"No======");
        }
    }
    completionHandler();
}


@end


